﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SmlEngine.Collections
{
    /// <summary>
    /// Extends the <see cref="System.Collections.Generic.Dictionary&lt;TKey, TValue&gt;"/> class.
    /// </summary>
    public static class DictionaryExtensions
    {
        /// <summary>
        /// Iterates over a Dictionary and applies an Action to each of the values.
        /// </summary>
        /// <typeparam name="TKey">The key type of the dictionary.</typeparam>
        /// <typeparam name="TValue">The value type of the dictionary.</typeparam>
        /// <param name="dictionary">The dictionary to iterate over.</param>
        /// <param name="action">The Action to execute.</param>
        public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, 
                                                 Action<TValue> action)
        {
            foreach (TValue value in dictionary.Values)
            {
                action(value);
            }
        }

        /// <summary>
        /// Iterates over a Dictionary and applies an Action to each of the keys.
        /// </summary>
        /// <typeparam name="TKey">The key type of the dictionary.</typeparam>
        /// <typeparam name="TValue">The value type of the dictionary.</typeparam>
        /// <param name="dictionary">The dictionary to iterate over.</param>
        /// <param name="action">The Action to execute.</param>
        public static void ForEachKey<TKey, TValue>(this Dictionary<TKey, TValue> dictionary,
                                                 Action<TKey> action)
        {
            foreach (TKey key in dictionary.Keys)
            {
                action(key);
            }
        }

        /// <summary>
        /// Iterates over a Dictionary and applies an Action to each of the keys.
        /// </summary>
        /// <typeparam name="TKey">The key type of the dictionary.</typeparam>
        /// <typeparam name="TValue">The value type of the dictionary.</typeparam>
        /// <param name="dictionary">The dictionary to iterate over.</param>
        /// <param name="action">The Action to execute.</param>
        public static void ForEach<TKey, TValue>(this Dictionary<TKey, TValue> dictionary,
                                                 Action<TKey, TValue> action)
        {
            foreach (KeyValuePair<TKey, TValue> pair in dictionary)
            {
                action(pair.Key, pair.Value);
            }
        }
    }
}
